package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.PageInfo;
import com.javasm.entity.MisMannageEntity.ProdSeriesInfo;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.SeriesService;
import com.javasm.service.MisMannageService.impl.SeriesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/22 19:54
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/series/*")
public class SeriesServlet extends BaseServlet {

    //查询
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        String querySeriesName = req.getParameter("querySeriesName");

        Integer page = 1;
        Integer pageSize = 5;

        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        SeriesService ss = new SeriesServiceImpl();
        List<ProdSeriesInfo> seriesByPage = ss.getSeriesByPage(page, pageSize, querySeriesName);
        Integer seriesTotalNum = ss.seriesTotalNum(querySeriesName);

        ReturnEntity re = new ReturnEntity();
        if (seriesByPage.size() > 0) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(seriesByPage);
            re.setPageInfo(new PageInfo(page,pageSize,seriesTotalNum));
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
            re.setPageInfo(new PageInfo(1,1,0));
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    //新增产品系列
    protected void addSeries(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String seriesSource = req.getParameter("seriesSource");
        String seriesTypeIdStr = req.getParameter("seriesTypeId");
        String seriesCHname = req.getParameter("seriesCHname");
        String seriesENname = req.getParameter("seriesENname");

        Integer seriesTypeId = 0;
        if (seriesTypeIdStr!=null && !"".equals(seriesTypeIdStr)){
            seriesTypeId = Integer.parseInt(seriesTypeIdStr);
        }

        ProdSeriesInfo addPsi = new ProdSeriesInfo();
        addPsi.setSeriesSource(seriesSource);
        addPsi.setTypeId(seriesTypeId);
        addPsi.setSeriesCHName(seriesCHname);
        addPsi.setSeriesENName(seriesENname);

        SeriesService ss = new SeriesServiceImpl();
        Integer addNum = ss.insertProdSeries(addPsi);

        ReturnEntity re = new ReturnEntity();
        if (addNum!=0){
            re.setRetCode(ReturnCode.ADD_OK.getCode());
            re.setRetMsg(ReturnCode.ADD_OK.getMsg());
        }else {
            re.setRetCode(ReturnCode.ADD_FAILED.getCode());
            re.setRetMsg(ReturnCode.ADD_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    //新增汇款信息(修改产品系列的汇款信息)
    protected void addRemitMsg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String bankName = req.getParameter("bankName");
        String swift = req.getParameter("swift");
        String bankCode = req.getParameter("bankCode");
        String cnaps = req.getParameter("cnaps");
        String bankAddr = req.getParameter("bankAddr");
        String bankCity = req.getParameter("bankCity");
        String payeeName = req.getParameter("payeeName");
        String payeeAccount = req.getParameter("payeeAccount");
        String payeeAddr = req.getParameter("payeeAddr");
        String remitMoneyCompany = req.getParameter("remitMoneyCompany");
        String seriesMsgIdStr = req.getParameter("seriesMsgId");

        Integer seriesMsgId = 0;
        if (seriesMsgIdStr!=null && !"".equals(seriesMsgIdStr)){
            seriesMsgId = Integer.parseInt(seriesMsgIdStr);
        }

        ProdSeriesInfo addRemitMsgPsi = new ProdSeriesInfo(seriesMsgId,remitMoneyCompany,bankName,bankAddr,bankCity,bankCode,swift,cnaps,payeeName,payeeAddr,payeeAccount);

        SeriesService ss = new SeriesServiceImpl();
        Integer addRemitMsgNum = ss.insertRemitMsg(addRemitMsgPsi,seriesMsgId);


        ReturnEntity re = new ReturnEntity();
        if (addRemitMsgNum!=0){
            re.setRetCode(ReturnCode.ADD_REMIT_MSG_OK.getCode());
            re.setRetMsg(ReturnCode.ADD_REMIT_MSG_OK.getMsg());
        }else {
            re.setRetCode(ReturnCode.ADD_REMIT_MSG_FAILED.getCode());
            re.setRetMsg(ReturnCode.ADD_REMIT_MSG_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void queryForEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryProdSeriesIdStr = req.getParameter("queryProdSeriesId");
        Integer queryProdSeriesId = 0;
        if (queryProdSeriesIdStr!=null && !"".equals(queryProdSeriesIdStr)){
            queryProdSeriesId = Integer.parseInt(queryProdSeriesIdStr);
        }
        SeriesService ss = new SeriesServiceImpl();
        ProdSeriesInfo prodSeriesInfo = ss.queryProdSeriesById(queryProdSeriesId);

        ReturnEntity re = new ReturnEntity();
        if (prodSeriesInfo!=null){
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(prodSeriesInfo);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
            re.setPageInfo(new PageInfo(1,0,1));
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void editProdSeries(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String seriesSource = req.getParameter("seriesSource");
        String seriesCHname = req.getParameter("seriesCHname");
        String seriesENname = req.getParameter("seriesENname");
        String seriesTypeIdStr = req.getParameter("seriesTypeId");
        String seriesIdStr = req.getParameter("seriesId");

        Integer seriesId = 0;
        Integer seriesTypeId = 0;
        if (seriesIdStr!=null && !"".equals(seriesIdStr)){
            seriesId = Integer.parseInt(seriesIdStr);
        }
        if (seriesTypeIdStr!=null && !"".equals(seriesTypeIdStr)){
            seriesTypeId = Integer.parseInt(seriesTypeIdStr);
        }

        ProdSeriesInfo editPsi = new ProdSeriesInfo();
        editPsi.setSeriesSource(seriesSource);
        editPsi.setSeriesCHName(seriesCHname);
        editPsi.setSeriesENName(seriesENname);
        editPsi.setTypeId(seriesTypeId);
        editPsi.setSeriesId(seriesId);

        SeriesService ss = new SeriesServiceImpl();
        Integer editNum = ss.editProdSeries(editPsi);

        ReturnEntity re = new ReturnEntity();
        if (editNum>0){
            re.setRetCode(ReturnCode.EDIT_OK.getCode());
            re.setRetMsg(ReturnCode.EDIT_OK.getMsg());
        } else {
            re.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            re.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();

    }
}
